<script type="text/javascript">
Ext.ns('catalog.catalog.adaugaMaterie');


catalog.catalog.adaugaMaterie.FormPanel =  Ext.extend(Ext.form.FormPanel, {
	id:'catalog.catalog.adaugaMaterie.FormPanel',
	userInformation:'Date profesor',
    lastName: 'Nume profesor',
    firstName: 'Prenume profesor',
    disciplineInformation:'Date materie',
    discipline:'Materie',
    class:'Clasa',
	initComponent:function() {
	    var config = {
	    	labelWidth: 150,
	    	frame : true,
	    	monitorValid:true,
	    	style:'padding:1px',
	    	items: [
		    {
            	        	xtype:'fieldset',
            	        	title: this.userInformation,
            	        	collapsible: true,
            		        autoHeight:true,
            		        defaults: {width: 210},
            		        defaultType: 'textfield',
            		        items :[ {
                                fieldLabel: this.lastName,
                                name: 'teacher.lastName',
                                allowBlank:false
                             }
            	            	,{
            		                fieldLabel: this.firstName,
            		                name: 'teacher.firstName',
            		                allowBlank:false
                                }
            		        ]
            		    } ,
            		    {
                            xtype:'fieldset',
                            title: this.disciplineInformation,
                            collapsible: true,
                            autoHeight:true,
                            defaults: {width: 210},
                            defaultType: 'textfield',
                            items :[
//                            new Ext.form.ComboBox({
//                             store:new Ext.data.JsonStore({
//                                 fields:[
//                                     {name:'id',type:'int'},
//                                      {name:'name' , type:'string'}
//                                 ],
//                                 url:'loadDiscipline.action'
//                                 }),
//                            displayField:'name',
//                            valueField:'id',
//                            name:'disciplineList',
//                            triggerAction:'all',
//                            fieldLabel:this.discipline,
//                            selectOnFocus:true
//                            }),
                               new Ext.form.ComboBox({
                                typeAhead: true,
                                triggerAction: 'all',
                                lazyRender:true,
                                name:'disciplineName',
                                fieldLabel:this.discipline,
                                mode: 'local',
                                store: new Ext.data.ArrayStore({
                                id: 0,
                                fields: [
                                'myId',
                                'displayText'
                                ],
                                data: [[1, 'Romana'], [2, 'Engleza'], [3, 'Franceza'],[4,'Chimie'],[5,'Fizica'],[6,'Matematica'],[7,'Informatica'],[8,'Istorie'],[9,'Geografie'],[10,'Biologie']]
                                }),
                                valueField: 'myId',
                                displayField: 'displayText',
                                value:1
                                }),
                                new Ext.form.ComboBox({
                                typeAhead: true,
                                triggerAction: 'all',
                                lazyRender:true,
                                name:'className',
                                fieldLabel:this.class,
                                mode: 'local',
                                store: new Ext.data.ArrayStore({
                                id: 0,
                                fields: [
                                'myId',
                                'displayText'
                                ],
                                data: [[1, '9A'], [2, '9B'], [3, '9C'], [4, '9D'],[5, '10A'], [6, '10B'], [7, '10C'], [8, '10D']]
                                }),
                                valueField: 'myId',
                                displayField: 'displayText',
                                value:1
                                })
                            ]
                        } ,
		  {
	    			buttonAlign:'center',
	    			buttons: [{
	    		        text: 'Asociaza',//mbLocale.submitButton,
	    		        scope:this,
	    		        formBind:true,
	    		        handler:function() {
	    			    	this.getForm().submit({
	    			            url: 'associateDiscipline.action',
	    			            method: 'POST',
	    			            waitMsg: mbLocale.waitingMsg,
	    			            success: function(form, action) {
	    			                obj = Ext.util.JSON.decode(action.response.responseText);

	    			                 if(obj.association==true)
	    			                showMsg('success', obj.message);
	    			                else
	    			                { var submitFun = function(buttonId, text, opt) {

                                                   if (buttonId == 'yes') {
                                                       catalog.util.ajaxRequest({
                                                           url: 'saveAssociation.action',
                                                           params:{
                                                          'teacher.lastName': obj.teacherLastName,
                                                          'teacher.firstName': obj.teacherFirstName,
                                                          'discipline.name': obj.discipline,
                                                          'schoolClass.name': obj.clasa
                                                         } ,
                                                           success:function(r,o){
                                                                    showMsg('success', obj.message);

                                                           },
                                                           failure:Ext.emptyFn
                                                       },this);
                                                   }
                                               };
                                               Ext.Msg.show({
                                                   title:mbLocale.infoMsg,
                                                   msg: 'Exista deja un profesor asociat la materia:'+obj.discipline+' la clasa '+obj.clasa+'. Doriti sa realizati noua asociere?',
                                                   buttons: Ext.Msg.YESNO,
                                                   scope: this,
                                                   fn: submitFun,
                                                   icon: Ext.MessageBox.QUESTION
                                               });

	    			                }
	    			            },
	    			            failure: function(form, action) {
	    			                obj = Ext.util.JSON.decode(action.response.responseText);
	    			                showMsg('failure', obj.message);
	    			            }
	    			    	});
	    				}
	    		    }]
		    	}],
		    	autoScroll:true
	    	}; // eof config object
		    // apply config
		    Ext.apply(this, Ext.apply(this.initialConfig, config));
		    // call parent
		    catalog.catalog.adaugaMaterie.FormPanel.superclass.initComponent.apply(this, arguments);

		},// eo funtion initComponent
        onRender:function(){
        catalog.catalog.adaugaMaterie.FormPanel.superclass.onRender.apply(this, arguments);
        this.form.load({
        url: 'loadDiscipline.action',
        success:function(f,a){
        Ext.each(a.result.settings,function(o,i){
        Ext.getCmp(o.name).setRawValue(o.value);
        Ext.fly('h_'+Ext.getCmp(o.name).getId()).dom.value=o.id;
        });
        }
        });
        }


});

try{ userSettingLocale(); } catch(e){}

Ext.onReady(function() {
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';

    var formPanel = new catalog.catalog.adaugaMaterie.FormPanel();
    if (mainPanel) {
        mainPanel.getActiveTab().add(formPanel);
        mainPanel.getActiveTab().doLayout();
    } else {
        var vp = new Ext.Viewport({layout:'fit', items:[formPanel]});
    }
});

</script>

